package com.yh.vo;

import com.yh.utils.Constants;
import lombok.Getter;
import lombok.ToString;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;

@Getter
@ToString
/**
 * 数据响应模型
 * @code 响应状态码，记录服务器的响应状态
 *      1000 表示账户不存在
 *      1001 表示密码错误
 *      2000 表示查询结果为空
 *      更多状态码详见'build'方法
 *
 * @author yuhuan
 * @date 2022/01/13
 * */
public class ResponseVO implements Serializable {
    private Integer code;
    private String msg;
    private Object data;

    private ResponseVO() {
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setData(Object data) {
        this.data = data;
    }

    /**
     * 快速构建一个响应实例。默认是构建一个响应200的实例对象
     *
     * @dataState 数据标记的状态。根据标记状态封装响应状态码和错误信息，如果传递的不是以下列出
     * 的任意值之一，均构建一个响应200的空实例返回。部分响应码如下：更多响应码详见常量类
     * -101  表示构建一个响应账户不存在的实例
     */
    public static ResponseVO buildSuccessInstance(Object data) {
        return buildSuccessInstance(Constants.ResponseDataState.RESP_SUCCESS,data);
    }

    public static ResponseVO buildSuccessInstance(int dataState,Object data) {
        InstanceBuild build = new InstanceBuild();
        return build.setCode(dataState).setMsg("OK").setData(data).build();
    }

    public static ResponseVO buildSuccessInstance(int dataState) {
        InstanceBuild build = new InstanceBuild();
        return build.build(dataState);
    }

    public static ResponseVO buildSuccessInstance(Integer dataState) {
        return buildSuccessInstance(dataState.intValue());
    }

    /**
     * 通过静态内部类构建对象
     */
    public static class InstanceBuild {
        private int code;
        private String msg;
        private Object data;

        public InstanceBuild setCode(int code) {
            this.code = code;
            return this;
        }

        public InstanceBuild setMsg(String msg) {
            this.msg = msg;
            return this;
        }

        public InstanceBuild setData(Object data) {
            this.data = data;
            return this;
        }

        public ResponseVO build() {
            // 如果没有设置响应状态就将响应码设置为200
            if (ObjectUtils.isEmpty(this.code) || this.code == 0) {
                this.code = 200;
            }
            return createResponseVO();
        }

        public ResponseVO build(int dataState) {
            if (internalElement(dataState,Constants.ResponseVoType.RESP_SHARED_TYPE)) {
                buildSharedResponseVO(dataState);
            }
            if (internalElement(dataState,Constants.ResponseVoType.RESP_SEARCH_TYPE)) {
                buildSearchResponseVO(dataState);
            }
            if (internalElement(dataState,Constants.ResponseVoType.RESP_ORDER_TYPE)) {
                buildOrderResponseVO(dataState);
            }
            if (internalElement(dataState,Constants.ResponseVoType.RESP_USER_TYPE)) {
                buildUserResponseVO(dataState);
            }
            if (internalElement(dataState,Constants.ResponseVoType.RESP_VALIDATOR_TYPE)) {
                buildValidatorResponseVO(dataState);
            }
            return build();
        }

        /**
         * 构建专用于搜索模块的响应VO
         */
        private void buildSearchResponseVO(int dataState) {
            if (dataState == Constants.ResponseDataState.INDEX_INIT_FAIL) {
                this.code = 2001;
                this.msg = "索引创建失败！请确认索引是否已经存在";
            }
            if (dataState == Constants.ResponseDataState.INDEX_DELETE_FAIL) {
                this.code = 2002;
                this.msg = "删除失败！请确认索引是否真实存在";
            }
        }

        /**
         * 构建专用于订单模块的响应VO
         */
        private void buildOrderResponseVO(int dataState) {
            if (dataState == Constants.ResponseDataState.PAY_SUCCESS) {
                this.code = 3000;
                this.msg = "OK,pay success";
                this.data = "payStatus.html";
            }
            if (dataState == Constants.ResponseDataState.FAILURE_ORDER) {
                this.code = 1002;
                this.msg = "订单创建失败";
            }
            if (dataState == Constants.ResponseDataState.PAY_FAIL_OF_REPEAT) {
                this.code = 3001;
                this.msg = "订单支付失败，请不要重复支付";
            }
            if (dataState == Constants.ResponseDataState.PAY_FAIL_OF_EXPIRE) {
                this.code = 3002;
                this.msg = "订单支付失败，超时支付";
            }
            if (dataState == Constants.ResponseDataState.DELETE_FAIL_OF_REFUNDING) {
                this.code = 3005;
                this.msg = "订单删除失败，请不要删除正在退款的订单";
            }
        }

        /**
         * 构建专用于用户模块的响应VO
         */
        private void buildUserResponseVO(int dataState) {
            if (dataState == Constants.ResponseDataState.NULL_ACCOUNT) {
                this.code = 1000;
                this.msg = "账户不存在";
            }
            if (dataState == Constants.ResponseDataState.PASSWORD_ERROR) {
                this.code = 1001;
                this.msg = "密码错误";
            }
            if (dataState == Constants.ResponseDataState.ILLEGAL_LOGIN) {
                this.code = 1003;
                this.msg = "非法的登录状态";
            }
            if (dataState == Constants.ResponseDataState.LOGIN_EXPIRED) {
                this.code = 1005;
                this.msg = "登录信息已过期";
                this.data = "login.html";
            }
            if (dataState == Constants.ResponseDataState.EXIST_ACCOUNT) {
                this.code = 1004;
                this.msg = "该账户已经被注册";
            }
        }

        /**
         * 构建通用的响应VO
         */
        private void buildSharedResponseVO(int dataState) {
            // 设置成功响应
            if (dataState == Constants.ResponseDataState.RESP_SUCCESS) {
                this.code = 200;
            }
            if (dataState == Constants.ResponseDataState.NULL_RESULT) {
                this.code = 2000;
                this.msg = "查询结果为空";
            }
            if (dataState == Constants.ResponseDataState.UPDATE_FAIL) {
                this.code = 3007;
                this.msg = "更新失败!";
            }
            if (dataState == Constants.ResponseDataState.DELETE_FAIL) {
                this.code = 3006;
                this.msg = "删除失败!";
            }
            if (dataState == Constants.ResponseDataState.DATA_IS_OCCUPIED) {
                this.code = 3008;
                this.msg = "数据被占用，无法删除!";
            }
            if (dataState == Constants.ResponseDataState.UPDATE_FAIL_EXIST) {
                this.code = 3009;
                this.msg = "更新失败!数据已经存在";
            }
        }

        /**
         * 构建专用于数据验证的响应VO
         */
        private void buildValidatorResponseVO(int dataState) {
            if (dataState == Constants.ValidatorResult.DATA_PASS) {
                this.code = 3100;
                this.msg = "数据格式验证通过";
            }
            if (dataState == Constants.ValidatorResult.EMAIL_ERROR) {
                this.code = 3102;
                this.msg = "邮箱格式错误";
            }
            if (dataState == Constants.ValidatorResult.MOBILE_ERROR) {
                this.code = 3103;
                this.msg = "手机格式错误";
            }
        }

        /**
         * 判断一个数字是否在指定区间内
         *
         * @param element 需要判断的数字
         * @param interval 指定的区间；来自Constants.ResponseVoType
         */
        private boolean internalElement(int element, int[] interval) {
            // 获取区间最小值
            int minInterval = interval[0];
            // 获取区间最大值
            int maxInterval = interval[1];
            return Math.max(minInterval,element) == Math.min(element,maxInterval);
        }

        private ResponseVO createResponseVO() {
            ResponseVO vo = new ResponseVO();
            vo.setCode(this.code);
            vo.setMsg(this.msg);
            vo.setData(this.data);
            return vo;
        }
    }
}
